import requests
from bs4 import BeautifulSoup
import traceback
import re

def getHTML(url):
    try:
        r = requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding =r.apparent_encoding
        return r.text
    except:
        return 0

def getStockList(lst,stockURL):
    html = getHTML(stockURL)
    soup = BeautifulSoup(html,'html.parser')
    a = soup.find_all('a')
    for i in a:
        try:
            href = i.attrs['href']
            lst.append(re.findall(r'hk.\d{5}',href)[0])
        except:
            continue


def getStockInfo(lst,stockURL,fpath):
    for stock in lst:
        stock = stock.strip('hk/')
        url = stockURL+'sz0'+stock+'.html'
        print(url)
        # html = getHTML(url)
        # try:
        #     if html=="":
        #         continue
        #     infoDict = {}
        #     soup = BeautifulSoup(html,'html.parser')
        #     stockinfo = soup.find('div',attrs={'class':"stock-bets"})
        #     print(type(stockinfo))

            # name = soup.find_all(attrs={'class':"bets-name"})[0]
            # infoDict.update({'股票名称':name.text.split()[0]})
            # KeyList = soup.find_all('dt')
            # valueList = soup.find_all('dd')
            # for i in range(len(KeyList)):
            #     key = KeyList[i].text
            #     val = valueList[i].text
            #     infoDict[key] = val
            #
            # with open(fpath,'a',encoding='utf-8') as f:
            #     f.write(str(infoDict)+'\n')
        # except:
        #     traceback.print_exc()
        #     continue

if __name__ == '__main__':
    stock_list_url = 'http://quote.eastmoney.com/hk/HStock_list.html'
    stock_Info_url = 'https://gupiao.baidu.com/stock/'
    output_file = 'C:/Users/kai/Desktop/邮科院学习/stock.txt'
    slist = []
    getStockList(slist,stock_list_url)
    getStockInfo(slist,stock_Info_url,output_file)

